package java6703.lesson05;

import java.util.*;

public class PointDemo6703 {
    public static void main(String[] args) {
        demoTreeSet();
        demoHashSet();
    demoList();
    }

    private static void demoTreeSet() {
        //添加信息
        Collection<Point6703> list = new ArrayList<Point6703>();
        list.add(new Point6703(1, 2));
        list.add(new Point6703(21, 31));
        list.add(new Point6703(11, 15));
        list.add(new Point6703(1, 2));
        list.add(new Point6703(21, 17));
        list.add(new Point6703(13, 15));
        System.out.println(list.contains(new Point6703(11, 15)));
    }

    private static void demoHashSet() {
        Collection<Point6703> list = new HashSet<Point6703>();
        list.add(new Point6703(1, 2));
        list.add(new Point6703(21, 31));
        list.add(new Point6703(11, 15));
        list.add(new Point6703(1, 2));
        list.add(new Point6703(21, 17));
        list.add(new Point6703(13, 15));
        System.out.println(list);
    }

    private static void demoList() {
        Collection<Point6703> list = new TreeSet<Point6703>();
        list.add(new Point6703(1, 2));
        list.add(new Point6703(21, 31));
        list.add(new Point6703(11, 15));
        list.add(new Point6703(1, 2));
        list.add(new Point6703(21, 17));
        list.add(new Point6703(13, 15));
        System.out.println(list);
    }

    private static class Point6703 implements Comparable<Point6703> {
        //匿名内部类

        private int x;
        private int y;

        //无参构造方法
        public Point6703() {
        }
        //带两个参数构造方法
        public Point6703(int x, int y) {
            this.x = x;
            this.y = y;
        }


        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        //重写toString方法
        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }

        //重写equals方法
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Point6703 point6703 = (Point6703) o;
            return x == point6703.x &&
                    y == point6703.y;
        }

        //重写hasCode方法
        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
        public int compareTo(Point6703 o) {
            if (this.x > o.x) {
                return 1;
            } else if (this.x < o.x) {
                return -1;
            } else {
                return y - o.y;
            }
        }
    }
}
